package com.truthrock.jpapg;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller // This means that this class is a Controller
@RequestMapping(path="/array") // This means URL's start with /demo (after Application path)
public class TestArrayController {
    @Autowired // This means to get the bean called userRepository
  // Which is auto-generated by Spring, we will use it to handle the data
  private TestArrayRepo arrayRepository;

  @PostMapping(path="/add") // Map ONLY POST Requests
  public @ResponseBody String addNewUser (@RequestParam String a1
      , @RequestParam String a2) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    TestArray t = new TestArray();
    List<String> list = new ArrayList<>();
    list.add(a1);
    list.add(a2);
    t.setA(list);
    arrayRepository.save(t);
    return "Saved";
  }

  @GetMapping(path="/all")
  public @ResponseBody Iterable<TestArray> getAllUsers() {
    // This returns a JSON or XML with the users
    return arrayRepository.findAll();
  }
}
